//
//  HotelViewController.m
//  Conference
//
//  Created by Xiang He on 12-11-19.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "HotelViewController.h"
#import "AppDelegate.h"
#import "Member.h"
#import "Globals.h"
#import "Constant.h"
#import "WebData.h"
#import "HotelData.h"
#import "NetContant.h"
@interface HotelViewController ()

@end

@implementation HotelViewController
@synthesize lab_shareInfo;
@synthesize lab_baseInfo;
@synthesize lab_phone;
@synthesize lab_address;
@synthesize hotelData;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    
    [super viewDidLoad];
    [self loadingData];
    // Do any additional setup after loading the view from its nib.
}
-(void)loadingData{
    Member *member= [(AppDelegate *)[[UIApplication sharedApplication] delegate]userMember];
    self.hotelData=member.hotelData;
    if (self.hotelData==nil) {
        NSThread *thread=[[[NSThread alloc] initWithTarget:self selector:@selector(run) object:nil] autorelease];
        [thread start];
    }else
        [self showData];
           
}
-(void)showData{
    NSString *name=self.hotelData.hotelName;
    NSString *type=self.hotelData.bookHotelFlag;
    NSString *checkTime=self.hotelData.checkTime;
    NSString *moveTime=self.hotelData.moveTime;
    NSString *payStatus=self.hotelData.payStatus;
    NSString *payMoney=self.hotelData.payMoney;
    NSString *hotelInfo=[NSString stringWithFormat:@"酒店名称:%@\r\n预定房型:%@\r\n入住时间:%@\r\n退房时间:%@\r\n支付情况:%@\r\n总金额:%@\r\n",name,type,checkTime,moveTime,payStatus,payMoney];
    
    CGSize size=[[Globals getInstance]CalculationCellSize:self.view.frame.size.width fontSize:16 content:hotelInfo];
    CGRect rect = [self.lab_baseInfo
                   textRectForBounds:self.lab_baseInfo.frame limitedToNumberOfLines:0];
    UIFont *font = [UIFont systemFontOfSize:16];
    rect.size=size;
    self.lab_baseInfo.frame = rect;
    self.lab_baseInfo.text=hotelInfo;
    self.lab_baseInfo.numberOfLines = 0;
    self.lab_baseInfo.font = font;
    
    if([@"" isEqualToString:self.hotelData.shareCustName])
        [self.lab_shareInfo setHidden:YES];
    else {
        NSString *shareInfo=[NSString stringWithFormat:@"姓名:%@\r\n公司:%@",self.hotelData.shareCustName,self.hotelData.shareCustCompany];
        self.lab_shareInfo.text=shareInfo;
        self.lab_shareInfo.numberOfLines = 0;
        self.lab_shareInfo.frame=CGRectMake(10, size.height+10, 320, 40);
    }
    
    self.lab_phone.text=self.hotelData.phone;
    self.lab_address.text=self.hotelData.hotleAddress;
}
-(void)run{
    NSAutoreleasePool *pool=[[NSAutoreleasePool alloc] init];
    NSString *sRet=[[WebData getInstance]getHotelInfo];
    [self performSelectorOnMainThread:@selector(onPostExecute:) withObject:sRet waitUntilDone:YES];
	[pool release];
    
}

-(void)onPostExecute:(NSString *)sRet{
    
    if ([sRet isEqualToString:[[NetContant getInstance] NET_CONNECT_FALSE]]) {
        
        
       // [[Globals getInstance] showAlertView:[[Constant getInstance] net_error]];
        
        
    }else if([sRet isEqualToString:[[NetContant getInstance] NETWORK_RECORD_ISEMPTY]]){
        
       // [[Globals getInstance] showAlertView:[[Constant getInstance] data_isempty]];
        
    }else {
        @try {
            [self doNetRet:sRet];
            [self doNetInfo];
        }
        @catch (NSException *exception) {
            
        }
        
        
    }
    
}

-(void)doNetRet:(NSString *)sRet{
    NSArray *tables=[sRet componentsSeparatedByString:[[NetContant getInstance]TABLES_SPLIT_FLAG]];
        if(nil!=[tables objectAtIndex:0]){
            NSArray *record=[[tables objectAtIndex:0]componentsSeparatedByString:[NetContant getInstance].RECORDS_SPLIT_FLAG];
            if([record count]>=11){
                HotelData *hoteldata=[[HotelData alloc]init];
                hoteldata.hotelName=[record objectAtIndex:0];
                hoteldata.phone = [record objectAtIndex:1];
                hoteldata.hotleAddress = [record objectAtIndex:2];
                hoteldata.cordX=[record objectAtIndex:3];
                hoteldata.cordY=[record objectAtIndex:4];
                hoteldata.roomType=[record objectAtIndex:5];
                hoteldata.checkTime=[record objectAtIndex:6];
                hoteldata.moveTime=[record objectAtIndex:7];
                hoteldata.bookHotelFlag=[record objectAtIndex:8];
                hoteldata.shareCustCompany=[record objectAtIndex:9];
                hoteldata.shareCustName=[record objectAtIndex:10];
                Member *member=[(AppDelegate *)[[UIApplication sharedApplication] delegate]userMember];
                member.hotelData=hoteldata;
                self.hotelData=hoteldata;
               [hoteldata release];
                
               
            }
        }
}
-(void)doNetInfo{
    [self showData];

}
- (void)viewDidUnload
{
    [self setHotelData:nil];
    [self setLab_phone:nil];
    [self setLab_address:nil];
    [self setLab_baseInfo:nil];
    [self setLab_shareInfo:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (void)dealloc {
    [hotelData release];
    [lab_phone release];
    [lab_address release];
    [lab_baseInfo release];
    [lab_shareInfo release];
    [super dealloc];
}
@end
